package com.myrpc.consumer.netty;

import java.util.concurrent.SynchronousQueue;

import com.alibaba.fastjson.JSON;
import com.myrpc.common.sessionchannel.Session;
import com.myrpc.common.vo.Response;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ClientHandler extends SimpleChannelInboundHandler<String>{

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
		
		Response response = JSON.parseObject(msg, Response.class);
		
		SynchronousQueue<Object> queue = Session.queueMap.get(response.getRequestId());
		Session.queueMap.remove(response.getRequestId());
		queue.put(response);
		
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		cause.printStackTrace();
		super.exceptionCaught(ctx, cause);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		//ctx.writeAndFlush("client");
	}

	
}
